package com.leTest.ucenterService.controller;

import com.leTest.JwtUtils;
import com.leTest.R;
import com.leTest.ucenterService.entity.UcenterMemberEntity;
import com.leTest.ucenterService.entity.vo.RegisterVo;
import com.leTest.ucenterService.service.UcenterMemberService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author le
 * @since 2025-02-25
 */
@RestController
@RequestMapping("/ucenterService/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMemberEntity member) {
        //业务层的登录方法login返回一个token值
        String token = memberService.login(member);
        return R.ok().data("token", token);
    }

    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法,该方法内部:根据request对象获取请求头中的token,然后就可以返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库,根据用户id得到用户信息
        UcenterMemberEntity member = memberService.getById(memberId);
        return R.ok().data("userInfo", member);
    }



}
